package top.jybill.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.jybill.domain.MyPageInfo;
import top.jybill.domain.MyUser;
import top.jybill.domain.ResponseData;
import top.jybill.domain.Role;
import top.jybill.service.IRoleService;
import top.jybill.service.Impl.RoleServiceImpl;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
  @Autowired
  private ResponseData res;
  @Autowired
  private IRoleService roleService;

  /**
   * 根据用户id查询所有角色
   * @param id
   * @return
   */
  @RequestMapping("/findRolesById")
  @ResponseBody
  public ResponseData findRolesById(String id) {
    res.clean();
    System.out.println(id);
    List<Role> roles = roleService.findRoleById(id);
    if (roles == null) {
      res.metaPut("status", 201);
      res.metaPut("msg", "无角色");
      res.dataPut("info", "无角色信息");
      return res;
    }
    res.metaPut("status", 200);
    res.metaPut("msg", "查询成功");
    res.dataPut("roles", roles);
    return res;
  }

  /**
   * 查询所有角色id
   * @return
   */
  @RequestMapping("/findAllRoles")
  @ResponseBody
  public ResponseData findAllRoles() {
    res.clean();
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    res.dataPut("roles", roleService.findAllRoles());
    return res;
  }

  /**
   * 查询符合page参数的roles
   * @return
   */
  @RequestMapping(value = "/findRolesByPage", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData findRolesByPage(@RequestBody MyPageInfo page) {
    res.clean();
    MyPageInfo retInfo = roleService.findRolesByPage(page);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    res.dataPut("info", retInfo);
    return res;
  }


  /**
   * 添加角色
   * @param role
   * @return
   */
  @RequestMapping(value = "/addRole", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData addRole(@RequestBody Role role) {
    res.clean();
    roleService.addRole(role);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 删除角色
   * @param rid
   * @return
   */
  @RequestMapping(value = "/delRole/{rid}", method = RequestMethod.DELETE)
  @ResponseBody
  public ResponseData delRole(@PathVariable("rid") String rid) {
    res.clean();
    roleService.delRole(rid);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 角色详细
   * @param rid
   * @return
   */
  @RequestMapping(value = "/findRoleByRid", method = RequestMethod.GET)
  @ResponseBody
  public ResponseData findRoleByRid(String rid) {
    res.clean();
    res.dataPut("role", roleService.findRolesByPage(rid));
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 更新角色
   * @param role
   * @return
   */
  @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData updateRole(@RequestBody Role role) {
    res.clean();
    roleService.updateRole(role);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }

  /**
   * 更新角色权限
   * @param role
   * @return
   */
  @RequestMapping(value = "/updateRolePermissions", method = RequestMethod.POST)
  @ResponseBody
  public ResponseData updateRolePermissions(@RequestBody Role role) {
    res.clean();
    roleService.updateRolePermissions(role);
    res.metaPut("status", 200);
    res.metaPut("msg", "OK");
    return res;
  }
}
